﻿using OpenTK.Graphics.OpenGL4;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Core.Rendering.Resources
{
    [DataContract]
    public class Material
    {
        public string TexturePath
        {
            get => _texturePath;
            set
            {
                _texturePath = value;
                Texture = Texture2D.Create(value);
            }
        }
        public string ShaderPath
        {
            get => _shaderPath;
            set
            {
                _shaderPath = value;
                Shader = Shader.Create(value);
            }
        }
        [DataMember] private string _texturePath;
        [DataMember] private string _shaderPath;
        public Texture2D Texture { get; set; }
        public Shader Shader { get; private set; }
        // public RasterizationSettings Rasterization = new();
        //public BlendableOptions Blendable = new();
        // public CullingOptions Culling = new();
        //public DepthFunction DepthFunction = new();
        //public ColorWritingOptions colorWriting = new();
        public Material(string texturePath = "./resources/objects/backpack/diffuse.jpg", string shaderPath = "./resources/Test.glsl")
        {
            _texturePath = texturePath;
            _shaderPath = shaderPath;
        }
        public static Material Create(string texturePath = "./resources/objects/backpack/diffuse.jpg", string shaderPath = "./resources/Test.glsl")
        {
            Material material = new Material(texturePath, shaderPath);
            material.Start();
            return material;
        }
        public void Start()
        {
            Texture = Texture2D.Create(TexturePath);
            Shader = Shader.Create(ShaderPath);
        }
        public void Bind()
        {
            Shader.Bind();
            Texture.Bind();
        }
    }
    [DataContract]
    public struct RasterizationSettings
    {
        [DataMember] public BlendableOptions BlendableOptions;
        [DataMember] public CullingOptions CullingOptions;
        [DataMember] public DepthTestOptions DepthTestOptions;
        [DataMember] public ColorWritingOptions ColorWritingOptions;
        public RasterizationSettings()
        {
            BlendableOptions = new BlendableOptions();
            CullingOptions = new CullingOptions();
            DepthTestOptions = new DepthTestOptions();
            ColorWritingOptions = new ColorWritingOptions();
        }
    }
    [DataContract]
    public struct BlendableOptions
    {
        [DataMember] public bool IsBlendable;
        [DataMember] BlendingFactor SFactor;
        [DataMember] BlendingFactor DFactor;
        public BlendableOptions()
        {
            IsBlendable = true;
            SFactor = BlendingFactor.SrcAlpha;
            DFactor = BlendingFactor.OneMinusSrcAlpha;
        }
    }
    [DataContract]
    public struct CullingOptions
    {
        [DataMember] public bool IsBackFaceCulling;
        [DataMember] public bool IsFrontFaceCulling;
        public CullingOptions()
        {
            IsBackFaceCulling = true;
            IsFrontFaceCulling = false;
        }
    }

    public struct DepthTestOptions
    {
        public bool IsEnableDepthTest;
        public bool IsEnableDepthWriting;
        public DepthFunction DepthFunction;
        public DepthTestOptions()
        {
            IsEnableDepthTest = true;
            IsEnableDepthWriting = false;
            DepthFunction = DepthFunction.Less;
        }
    }
    [DataContract]
    public struct ColorWritingOptions
    {
        [DataMember] public bool R;
        [DataMember] public bool G;
        [DataMember] public bool B;
        [DataMember] public bool A;
        public ColorWritingOptions()
        {
            R = G = B = A = true;
        }
    }
}